{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parallel computing on multivectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Distributed\n",
    "addprocs(4);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "@everywhere using JOLI\n",
    "@everywhere using LinearAlgebra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Setup problem size, define operator, and input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "M=8;\n",
    "NVC=15;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: joLinearFunction\n",
      "Name: joDFT\n",
      "Size: (8, 8)\n",
      " DDT: Float64\n",
      " RDT: Complex{Float64}\n"
     ]
    }
   ],
   "source": [
    "A=joDFT(M,planned=false);\n",
    "show(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8×15 Array{Float64,2}:\n",
       " -0.233567   1.3582    0.573466   …   0.682527  -0.970062    0.679522\n",
       " -0.658011  -0.155475  0.896616      -0.923747  -1.70329    -0.992734\n",
       "  0.127603  -0.253827  2.1606         0.471299  -0.272386    1.04764 \n",
       "  0.618609  -0.474054  0.739256       0.192607   1.51645     1.17235 \n",
       "  0.481497   0.995508  1.70796        0.666186   0.883242    0.795031\n",
       "  0.677615  -0.595183  0.937017   …   1.19306   -0.166836    1.05681 \n",
       " -0.534713   0.628026  0.0629796      0.285508   0.24773     0.405323\n",
       "  0.681263   0.561292  1.99202       -0.376353  -0.0886528  -1.17941 "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=randn(M,NVC)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Shared Ararys"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Define parallel setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "joPAsetup: joPAsetup\n",
      " DataType: Float64\n",
      " Dims    : (8, 15)\n",
      " Chunks  : [1, 4]\n",
      " Workers : [2, 3, 4, 5]\n",
      "  Worker/ranges:   2 (1:8, 1:4)\n",
      "  Worker/ranges:   3 (1:8, 5:8)\n",
      "  Worker/ranges:   4 (1:8, 9:12)\n",
      "  Worker/ranges:   5 (1:8, 13:15)\n"
     ]
    }
   ],
   "source": [
    "ps=joPAsetup((M,NVC));\n",
    "show(ps)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Define parallel operator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: joSAdistributedLinearOperator\n",
      "Name: joSAdistributedLinearOperator(joDFT)\n",
      "Size: (8, 8)\n",
      " NVC: 15\n",
      " DDT: Float64\n",
      " RDT: Complex{Float64}\n"
     ]
    }
   ],
   "source": [
    "pA=joSAdistributedLinOp(A,ps);\n",
    "show(pA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Define shared array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8×15 SharedArrays.SharedArray{Float64,2}:\n",
       " -0.233567   1.3582    0.573466   …   0.682527  -0.970062    0.679522\n",
       " -0.658011  -0.155475  0.896616      -0.923747  -1.70329    -0.992734\n",
       "  0.127603  -0.253827  2.1606         0.471299  -0.272386    1.04764 \n",
       "  0.618609  -0.474054  0.739256       0.192607   1.51645     1.17235 \n",
       "  0.481497   0.995508  1.70796        0.666186   0.883242    0.795031\n",
       "  0.677615  -0.595183  0.937017   …   1.19306   -0.166836    1.05681 \n",
       " -0.534713   0.628026  0.0629796      0.285508   0.24773     0.405323\n",
       "  0.681263   0.561292  1.99202       -0.376353  -0.0886528  -1.17941 "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "px=salloc(ps)\n",
    "px[:,:]=x[:,:]\n",
    "px"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Just parallel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8×15 SharedArrays.SharedArray{Complex{Float64},2}:\n",
       "  0.410226+0.0im        0.729905+0.0im       …   1.05519+0.0im      \n",
       " -0.571056+0.115406im   0.496993+0.460691im     -1.14116-0.302648im \n",
       "  0.231592+0.452643im    0.69986+0.296241im     0.007632-0.0251496im\n",
       "   0.06543+0.583734im  -0.240534-0.162873im      1.05949+0.15154im  \n",
       " -0.522783+0.0im         1.19901+0.0im           1.01488+0.0im      \n",
       "   0.06543-0.583734im  -0.240534+0.162873im  …   1.05949-0.15154im  \n",
       "  0.231592-0.452643im    0.69986-0.296241im     0.007632+0.0251496im\n",
       " -0.571056-0.115406im   0.496993-0.460691im     -1.14116+0.302648im "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "py=pA*px"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Serial to serial via parallel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Define distrtibuting and gathering operators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: joSAdistribute\n",
      "Name: joSAdistributeMV:15\n",
      "Size: (8, 8)\n",
      " NVC: 15\n",
      " DDT: Float64\n",
      " RDT: Float64\n"
     ]
    }
   ],
   "source": [
    "pD=joSAdistribute(pA);\n",
    "show(pD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: joSAgather\n",
      "Name: joSAgatherMV:15\n",
      "Size: (8, 8)\n",
      " NVC: 15\n",
      " DDT: Complex{Float64}\n",
      " RDT: Complex{Float64}\n"
     ]
    }
   ],
   "source": [
    "pG=joSAgather(pA);\n",
    "show(pG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Distribute data, apply operator, gather data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8×15 SharedArrays.SharedArray{Float64,2}:\n",
       " -0.233567   1.3582    0.573466   …   0.682527  -0.970062    0.679522\n",
       " -0.658011  -0.155475  0.896616      -0.923747  -1.70329    -0.992734\n",
       "  0.127603  -0.253827  2.1606         0.471299  -0.272386    1.04764 \n",
       "  0.618609  -0.474054  0.739256       0.192607   1.51645     1.17235 \n",
       "  0.481497   0.995508  1.70796        0.666186   0.883242    0.795031\n",
       "  0.677615  -0.595183  0.937017   …   1.19306   -0.166836    1.05681 \n",
       " -0.534713   0.628026  0.0629796      0.285508   0.24773     0.405323\n",
       "  0.681263   0.561292  1.99202       -0.376353  -0.0886528  -1.17941 "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "px=pD*x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8×15 SharedArrays.SharedArray{Complex{Float64},2}:\n",
       "  0.410226+0.0im        0.729905+0.0im       …   1.05519+0.0im      \n",
       " -0.571056+0.115406im   0.496993+0.460691im     -1.14116-0.302648im \n",
       "  0.231592+0.452643im    0.69986+0.296241im     0.007632-0.0251496im\n",
       "   0.06543+0.583734im  -0.240534-0.162873im      1.05949+0.15154im  \n",
       " -0.522783+0.0im         1.19901+0.0im           1.01488+0.0im      \n",
       "   0.06543-0.583734im  -0.240534+0.162873im  …   1.05949-0.15154im  \n",
       "  0.231592-0.452643im    0.69986-0.296241im     0.007632+0.0251496im\n",
       " -0.571056-0.115406im   0.496993-0.460691im     -1.14116+0.302648im "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "py=pA*px"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8×15 Array{Complex{Float64},2}:\n",
       "  0.410226+0.0im        0.729905+0.0im       …   1.05519+0.0im      \n",
       " -0.571056+0.115406im   0.496993+0.460691im     -1.14116-0.302648im \n",
       "  0.231592+0.452643im    0.69986+0.296241im     0.007632-0.0251496im\n",
       "   0.06543+0.583734im  -0.240534-0.162873im      1.05949+0.15154im  \n",
       " -0.522783+0.0im         1.19901+0.0im           1.01488+0.0im      \n",
       "   0.06543-0.583734im  -0.240534+0.162873im  …   1.05949-0.15154im  \n",
       "  0.231592-0.452643im    0.69986-0.296241im     0.007632+0.0251496im\n",
       " -0.571056-0.115406im   0.496993-0.460691im     -1.14116+0.302648im "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y=pG*py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Or all together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: joLinearOperator\n",
      "Name: ((joSAgatherMV:15*joSAdistributedLinearOperator(joDFT))*joSAdistributeMV:15)\n",
      "Size: (8, 8)\n",
      " DDT: Float64\n",
      " RDT: Complex{Float64}\n"
     ]
    }
   ],
   "source": [
    "ps2s=pG*pA*pD\n",
    "show(ps2s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8×15 Array{Complex{Float64},2}:\n",
       "  0.410226+0.0im        0.729905+0.0im       …   1.05519+0.0im      \n",
       " -0.571056+0.115406im   0.496993+0.460691im     -1.14116-0.302648im \n",
       "  0.231592+0.452643im    0.69986+0.296241im     0.007632-0.0251496im\n",
       "   0.06543+0.583734im  -0.240534-0.162873im      1.05949+0.15154im  \n",
       " -0.522783+0.0im         1.19901+0.0im           1.01488+0.0im      \n",
       "   0.06543-0.583734im  -0.240534+0.162873im  …   1.05949-0.15154im  \n",
       "  0.231592-0.452643im    0.69986-0.296241im     0.007632+0.0251496im\n",
       " -0.571056-0.115406im   0.496993-0.460691im     -1.14116+0.302648im "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y=ps2s*x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.3.1",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
